# !/usr/bin python3
# encoding: utf-8 -*-
# @file     : db_util.py
# @author   : 沙陌 Matongxue_2
# @Time     : 2023/12/26 20:01
# @Copyright: 北京码同学
import pymysql


class DBUtil:

    def __init__(self,host,user,password,port=3306):
        self.connect = pymysql.Connect(
            host = host,
            port=port,
            user=user,
            password=password,
            charset='utf8mb4',
            cursorclass=pymysql.cursors.DictCursor # 这个参数得意思是把得到的数据行当做字典
        )

    def select(self,sql):
        cursor = self.connect.cursor()
        cursor.execute(sql)
        data = cursor.fetchall()
        self.connect.commit() # 提交事务,如果不提交，那么下次查询依然是旧数据
        cursor.close()
        return data
    def update(self,sql):
        """
        insert/update/delete
        :param sql:
        :return:
        """
        cursor = self.connect.cursor()
        cursor.execute(sql)
        self.connect.commit()
        cursor.close()
    def close(self):
        if self.connect!=None:
            self.connect.close()

if __name__ == '__main__':
    db_util = DBUtil(
        host = '59.36.173.55',
        port=3306,
        user='mtxshop_test',
        password='mtxshamo'
    )
    res = db_util.select('SELECT * FROM mtxshop_trade.es_order eo WHERE member_id=59 order by order_id desc limit 1')
    print(res[0])
    print(res[0]['order_status'])
    db_util.close()